package com.exchange.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exchange.entity.model.ExchangeRecordModel;
import com.exchange.entity.pojo.ExchangeRecord;
import com.exchange.mapper.ExchangeRecordMapper;
import com.exchange.service.ExchangeRecordService;
import com.exchange.until.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Administrator
 * @description 针对表【exchange_record】的数据库操作Service实现
 * @createDate 2024-04-25 20:02:10
 */
@Service
public class ExchangeRecordServiceImpl extends ServiceImpl<ExchangeRecordMapper, ExchangeRecord>
        implements ExchangeRecordService {
    @Autowired
    private ExchangeRecordMapper exchangeRecordMapper;

    @Override
    public List<String> findAddressList() {
        return exchangeRecordMapper.findAddressList();
    }

    @Override
    public Result<Map<String, List>> findAddressAndDisplay(Integer day) {
        List<String> exchangeRecordList = new ArrayList<>();
        List<Integer> exchangeRecordCount = new ArrayList<>();
        QueryWrapper<ExchangeRecord> exchangeRecordQueryWrapper = new QueryWrapper<>();
        if (day==0){

        }else if (day==1){
            DateTime dateTime = DateUtil.yesterday();
            exchangeRecordQueryWrapper.between("create_time",dateTime,new Date());
        }else if (day==7){
            DateTime dateTime = DateUtil.lastWeek();
            exchangeRecordQueryWrapper.between("create_time",dateTime,new Date());
        }
        List<ExchangeRecord> exchangeRecords = this.exchangeRecordMapper.selectList(exchangeRecordQueryWrapper);
        boolean b = false;
        for (ExchangeRecord ex : exchangeRecords) {
            b = false;
            int temp = 0;
            for (String exchangeModel : exchangeRecordList) {
                if (ex.getAddress().equals(exchangeModel)) {
                    exchangeRecordCount.set(temp, exchangeRecordCount.get(temp) + 1);
                    b = true;
                }
                temp++;
            }
            if (!b) {
                exchangeRecordList.add(ex.getAddress());
                exchangeRecordCount.add(1);
            }
        }
        java.util.Map<String,List> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("addressList", exchangeRecordList);
        objectObjectHashMap.put("countList", exchangeRecordCount);
        return Result.success(objectObjectHashMap);
    }
}




